package com.jahepi.android.componente;

import com.jahepi.android.R;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

public class TecladoNumerico2 extends LinearLayout implements OnClickListener, TecladoNumericoInterface {
	
	private String codigo = "";
	private Button button0, button1, button2, button3, button4, button5, button6, button7, button8, button9;
	private ImageButton buttonBack, buttonClean;
	private TextView codigoTextView;
	
	public TecladoNumerico2(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		setOrientation(VERTICAL);
		
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.tecladonumerico2, this);
        
        codigoTextView = (TextView) findViewById(R.id.codigoTeclado);
        button0 = (Button) findViewById(R.id.button0);
        button1 = (Button) findViewById(R.id.button1);
        button2 = (Button) findViewById(R.id.button2);
        button3 = (Button) findViewById(R.id.button3);
        button4 = (Button) findViewById(R.id.button4);
        button5 = (Button) findViewById(R.id.button5);
        button6 = (Button) findViewById(R.id.button6);
        button7 = (Button) findViewById(R.id.button7);
        button8 = (Button) findViewById(R.id.button8);
        button9 = (Button) findViewById(R.id.button9);
        buttonBack = (ImageButton) findViewById(R.id.buttonBack);
        buttonClean = (ImageButton) findViewById(R.id.buttonClean);
        
        button0.setOnClickListener(this);
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        button4.setOnClickListener(this);
        button5.setOnClickListener(this);
        button6.setOnClickListener(this);
        button7.setOnClickListener(this);
        button8.setOnClickListener(this);
        button9.setOnClickListener(this);
        
        buttonClean.setOnClickListener(new OnClickListener() {			
			public void onClick(View v) {
				codigo = "";
				codigoTextView.setText(obtenerFormatoTextView());
			}
		});
        
        buttonBack.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if(codigo.length() >= 1) {
					codigo = codigo.substring(0, codigo.length()-1);
				}
				codigoTextView.setText(obtenerFormatoTextView());
			}
		});
	}

	public void onClick(View v) {
		Button button = (Button) v;	
		if(codigo.length() < 4) {
			codigo += button.getText().toString();
			codigoTextView.setText(obtenerFormatoTextView());
		}
	}
	
	public int obtenerNumero() {
		try {
			return Integer.parseInt(codigo);
		} catch(NumberFormatException e) {
			return 0;
		}
	}
	
	private String obtenerFormatoTextView() {
		if(codigo.length() == 0) {
			return "0000";
		} else if(codigo.length() == 1) {
			return "000" + codigo;
		} else if(codigo.length() == 2) {
			return "00" + codigo;
		} else if(codigo.length() == 3) {
			return "0" + codigo;
		} else {
			return codigo;
		}
	}

}
